package com.glory.dmegcpda.view

import android.content.Intent
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glory.dmegcpda.R
import com.glory.dmegcpda.base.Constant


/**
 * FileName: SkipDialogActivity
 * Author: XJH
 * Date: 2021/7/22 17:13
 * Description:
 * History:
 */
class SkipDialogActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }


    private fun initView() {
        val extra = intent.getStringExtra("type")
        when {
            extra!!.contains("server_address") -> {
                val addressDialog = getAddressDialog("server_address", object : OnAddressDialog {
                    override fun onAddress(address: String) {
                        val intent = Intent()
                        intent.putExtra("server_address", address)
                        setResult(1, intent)
                    }
                })
                addressDialog.show()
            }
            extra.contains("file_address") -> {
                val addressDialog = getAddressDialog("file_address", object : OnAddressDialog {
                    override fun onAddress(address: String) {
                        val intent = Intent()
                        intent.putExtra("file_address", address)
                        setResult(2, intent)
                    }
                })
                addressDialog.show()
            }

        }
    }


    private fun getAddressDialog(ress: String, address: OnAddressDialog): MaterialDialog {
        val dialog = MaterialDialog(this)
            .cancelable(false)
            .cornerRadius(12f)
            .customView(R.layout.dialog_address, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()

        val ivClose: ImageView = contentView.findViewById(R.id.ivClose)
        val add_no: TextView = contentView.findViewById(R.id.add_no)
        val add_yes: TextView = contentView.findViewById(R.id.add_yes)
        val add_text: TextView = contentView.findViewById(R.id.add_text)
        val etEqpWorkID: EditText = contentView.findViewById(R.id.etEqpWorkID)
        when (ress) {
            "server_address" -> {
                add_text.text = "服务器地址："
                etEqpWorkID.setText(Constant.mUrl)
            }
            "file_address" -> {
                add_text.text = "文件地址："
            }

        }


        ivClose.setOnClickListener {
            dialog.dismiss()
            finish()
        }
        add_no.setOnClickListener {
            dialog.dismiss()
            finish()
        }
        add_yes.setOnClickListener {
            address.onAddress(etEqpWorkID.text.toString())
            dialog.dismiss()
            finish()
        }

        return dialog
    }

    interface OnAddressDialog {
        fun onAddress(address: String)
    }

    override fun onPause() {
        super.onPause()
        finish()
    }
}